package com.ysu.yybk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysu.yybk.dto.GroupDTO;
import com.ysu.yybk.dto.GroupListDTO;
import com.ysu.yybk.dto.GroupStudentDTO;
import com.ysu.yybk.entity.discuss.Discuss;
import com.ysu.yybk.entity.discuss.DiscussGroup;
import com.ysu.yybk.entity.discuss.DiscussGroupStudent;
import com.ysu.yybk.entity.discuss.DiscussItem;
import com.ysu.yybk.enums.ResultEnum;
import com.ysu.yybk.exception.YybkException;
import com.ysu.yybk.mapper.discuss.DiscussGroupMapper;
import com.ysu.yybk.mapper.discuss.DiscussGroupStudentMapper;
import com.ysu.yybk.mapper.discuss.DiscussItemMapper;
import com.ysu.yybk.mapper.discuss.DiscussMapper;
import com.ysu.yybk.service.DiscussStudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DiscussStudentServiceImpl implements DiscussStudentService {

    @Autowired
    private DiscussMapper discussMapper;

    @Autowired
    private DiscussGroupMapper groupMapper;

    @Autowired
    private DiscussGroupStudentMapper groupStudentMapper;

    @Autowired
    private DiscussItemMapper itemMapper;

    @Override
    public GroupDTO groupList(Integer discussId, String studentId) {
        // 判断该讨论是否存在  有点慢啊
        Discuss discuss = discussMapper.selectById(discussId);
        if (discuss == null) {
            throw new YybkException(ResultEnum.DISCUSS_NOT_EXIST);
        }
        GroupDTO groupDTO = new GroupDTO();
        // 查询该讨论所有组
        LambdaQueryWrapper<DiscussGroup> groupWrapper = new LambdaQueryWrapper<>();
        groupWrapper.eq(DiscussGroup::getDiscussId, discussId);
        List<DiscussGroup> groupList = groupMapper.selectList(groupWrapper);
        // 查询小组信息
        List<GroupListDTO> groupListDTOList = new ArrayList<>();
        for (int i = 0; i < groupList.size(); i++) {
            GroupListDTO groupListDTO = new GroupListDTO();
            groupListDTO.setGroupId(groupList.get(i).getId());
            groupListDTO.setGroupName(groupList.get(i).getName());
            // 构造查询组内学生对象
            LambdaQueryWrapper<DiscussGroupStudent> groupStudentWrapper = new LambdaQueryWrapper<>();
            groupStudentWrapper.eq(DiscussGroupStudent::getDiscussId, discussId).eq(DiscussGroupStudent::getGroupId, groupList.get(i).getId());
            List<DiscussGroupStudent> groupStudentList = groupStudentMapper.selectList(groupStudentWrapper);
            // 将查询结果放入list之中
            List<GroupStudentDTO> groupStudentDTOList = new ArrayList<>();
            for (DiscussGroupStudent groupStudent : groupStudentList) {
                GroupStudentDTO groupStudentDTO = new GroupStudentDTO();
                BeanUtils.copyProperties(groupStudent, groupStudentDTO);
                groupStudentDTOList.add(groupStudentDTO);
                if (groupStudent.getStudentId().equals(studentId)) {
                    groupDTO.setGroupId(i);
                    // 查询学生提交的答案
                    LambdaQueryWrapper<DiscussItem> itemWrapper = new LambdaQueryWrapper<>();
                    itemWrapper.eq(DiscussItem::getDiscussId, discussId).eq(DiscussItem::getGroupId, groupList.get(i).getId());
                    DiscussItem discussItem = itemMapper.selectOne(itemWrapper);
                    if (discussItem != null) {
                        groupDTO.setAnswer(discussItem.getAnswer());
                        groupDTO.setUrl(discussItem.getUrl());
                        groupDTO.setScore(discussItem.getScore());
                    }
                }
            }
            groupListDTO.setGroupStudentDTOList(groupStudentDTOList);
            groupListDTOList.add(groupListDTO);
        }
        groupDTO.setGroupListDTOList(groupListDTOList);
        return groupDTO;
    }
}
